PreLab 05

Exceptional Pictures and Images
Due by the beginning of lecture on Wednesday, September 27th

In this prelab you will formulate some of the ideas necessary to complete Lab 05. Note that there are 4 nuumbered questions for you to answer.

Using Exceptions

1. Why do we use try/except instead of if statements to deal with problems of opening missing or bad files?

2. The following three lines of code ask the user to input two points on a line, then they print the line's' slope.

   x1, y1, x2, y2 = eval(input("Enter x1,y1,x2,y2, separapted by commas"))
   slope = (y2-y1)/(x2-x1)
   print( "The slope of that line is %.2f"% slope ) 
If the user only supplies 3 numbers this gives a ValueError exception; if x1 and x2 are the same this gives a ZeroDivisionError exception. Write a block of code containing these three lines that prompts the user over and over until it gets 4 coordinate making two points with a valid slope. Use a try/except statement to keep the program from crashing on bad input.



Image Manipulation

In this lab you'll create a nifty program that reads in an image and lets the user do modifications of that image. Things like inverting the image, mirroring it, increasing or decreasing the contrast, etc.

3. You will need to write command interpreter. Let's get a start on that. Write a main() function that over and over expects the user to input one of three words: "copy", "flip" or "quit". On input "copy" it calls function copy( ) (with no arguments for now); you don't need to write function copy( ) now. On input "flip" it calls function flip(), and on input "quit" it halts. You should ignore any other input.

Your program should be capable of the following operations:

  1. Copy the image
  2. Flip the image horizontally
  3. Change the image to grayscale
  4. Scroll the image horizontally
  5. Negate the colors
  6. Zoom in on the center of toheo image
  7. Posterize the colors
  8. Rotate the image 180 degrees
  9. Blur the image

Examples of most of these operations are given below.

Original Image:
original
Flip: Scroll:
flip scroll
Negative: Zoom:
negative zoom
Posterize: Rotate 180:
poster rotate 180
Grayscale: Blur:
grayscale blur


Here are quick descriptions of these:

Copy: Copy produces a new image that is the same size as the original, with the same color at each pixel.

Flip: This flips the image horizontally, taking the color from pixel (0, y) in the original to pixel (w-1, y) in the new image, the color from (1, y) in the original to (w-2, y) in the new, and so forth.

Grayscale: Shades of gray have the same red, green and blue value. There are many ways to convert a color value to a gray value. The way televisions convert a color signal to gray (which we call "black and white") is to let (r,g,b) be the color components of a pixel and let gray=0.299*r+0.587*g+0.114*b. This pixel in the new image gets color (gray, gray, gray). The numbers 0.299, 0.587 etc. in this formula come from measurments of the average human eye sensitivity to different primary colors.

Scrolling: Scrolling should ask the user to specify some number of pixels, and should then shift the image that many to the right. Pixels that would fall off the edge of the image should wrap around to the other size. Modular arithmetic (the % operator) may come in handy here.

Negatives: The negative of an image is creating by inverting each color channel. So if the red value of a pixel were 255, it should become 0. If it were 254, it should become 1, and so on, down to 0, which should become 255. Similarly for green and blue.

Zoom: This method result in an image of the same size as the original, but consist of the center of the image blown up by a factor of 2. So if the image has width w and height h, zooming should expand the middle w/2 by h/2 region to fill the whole picture.

Posterize: A typical pixel can have one of 256 values for each color channel. In a posterized image, this number is drastically decreased. Each color channel value should be rounded to the nearest multiple of 64. In other words, the value of each channel must be one of 0, 64, 128, 192, 255.

Blur: When you blur an image, you set the color of each pixel to be the average of the 25 pixels in the 5 x 5 square centered at that pixel (i.e. the average of the original pixel and its original 24 neighbors). Be careful at the borders, not all pixels have 24 neighbors!

Here is my copy( ) function:


   def copy(pic):
         w = pic.getWidth()
         h = pic.getHeight()
         pic2 = picture.Picture(w, h)
         for x in range(0,w):
              for y in range(0,h):
                   r = pic.getPixelRed(x,y)
                   g = pic.getPixelGreen(x,y)
                   b = pic.getPixelBlue(x,y)
                   pic2.setPixelRed(x, y, r)
                   pic2.setPixelGreen(x,y,g)
                   pic2.setPixelBlue(x,y,b)
         pic2.setTitle("Copy")
         return pic2

It should be easy to follow this code. Variable pic holds an image. pic.getWidth() and pic.getHeight() return the image's dimensions. pic.getPixelRed(xy) get the value of the red primary color at pixel (x,y) ofthe image pic; pic2.setPixelRed(x,y,r) sets the red primary value of pixel (x,y) in image pic2> too r. Finally, pic2.setTitle("Copy") writes the string "Copy" in the bar at thetop of the window containing pic2. You will have a number of images displayed at the same time with this program; putting the name of the function that made an image into its window will help you keep them straight.

4. Write the flip() function. It should look very much like the copy( ) function above; the only difference is where you write the colors you find in pixel (x,y) of the original image pic.

Honor Code

If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your homework.

I affirm that I have adhered to the Honor Code in this assignment.